﻿using System.Collections.Generic;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Configuration.Actions;
using PSE.Deployment.SetupBase.Parameter;
using System;
using System.Text;
namespace PSE.Deployment.ConfigurationManager.Engine.Operations
{
    /// <summary>
    /// Classe abstrata que possui elementos basicos de Operacao
    /// </summary>
    public abstract class BaseOperations
    {
        private const string SinacorPrivateKey = "<RSAKeyValue><Modulus>2hhqrGyK5Mx9GccfBaSzvik5FBt/ipcpqcra+0ra0HUSgwqfonX+R4TAEvZCMVAEeUeWySBnRQ1kdEpKG1bS1snnw1VmQG6cdvqeyvyr5776xZQIy+bwaMq1brozScaUAb3ux9ka3e1pZFXUZCd+ttXwDO8OSeJnsdb3fgody78=</Modulus><Exponent>AQAB</Exponent><P>9RQ6yknoAgOebyTJ+UfJwxqO4FqBpsbsrb+KJb/pMTSyTUlf1AhB0PVtR1OavQ1GAb7CftP+2DnGxa12HfqrxQ==</P><Q>49BeHy+lDJPUlhqm3F+iOg4mIXyR6PJsU/II/cTzRmIXV3xhrMQODaGYikjyURBsZXv8TyvQPDCc07v9yx79sw==</Q><DP>qWXhOP+aqQG9Wg9wt6hCMmRCC5sjcJag83uoTm13Drbg44DrBtDqRTfO3/VOyIvLLtue7jE0Plq5JNu///rLDQ==</DP><DQ>IJ3Ji/9/SvCzZBZGw5Bxoow6C1tRbJ+gwNgLT4HuLqqGmwD03w5m0K7NLydIgjCuxEzfD9q/Ao8XNP1aVcWHHw==</DQ><InverseQ>UB2v2m+XZrgL6L36j0vbaCE/UPOMIJNlR2ZNRxhHwYQqE+1oTJkctSYnfMvev7LRtwqvhqrTeenVvtARBipC9A==</InverseQ><D>bAmmZk3KrDbETg2+x1vkknH3TYelsbDaObs8qTlWlec4yWNs2tzanvEa+Qm2lpkN7kpRXsbHgYR128mlVk+3VzchG1WlRuJs+Z4GozEq3meQORdCvA+wPgiU+aV2OL3lYHHOSc9a8rJS8fFLfCmdTsXM2Kw1cQ4zyhtiDpB2bMk=</D></RSAKeyValue>";
        public SetupParameter Parameter { get; set; }

        public BaseOperations(SetupParameter parameter)
        {
            Parameter = parameter;
        }

        public void EncryptTags(List<XmlTag> tags)
        {
            foreach (XmlTag tag in tags)
            {
                if (tag.IsEncrypted && !string.IsNullOrEmpty(tag.Value))
                {
                    bool hasChanges = tag.HasChanges;

                    if (tag.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_Generic)
                    {
                        tag.Value = PSE.Framework.Cryptography.RijndaelCryptographer.EncryptString(tag.Value);
                    }
                    else if (tag.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.RSA_Infra)
                    {
                        tag.Value = PSE.Framework.Cryptography.RSACryptographer.EncryptString(tag.Value, tag.AlgorithmPublicKey);
                    }
                    else if (tag.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_InfraData)
                    {
                        tag.Value = PSE.Deployment.Util.EncryptDecrypt.Encrypt(tag.Value);
                    }
                    else if (tag.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.MD5)
                    {
                        var hasher = new PSE.Framework.Cryptography.HashCryptographer(PSE.Framework.Cryptography.HashCryptographer.Algorithm.MD5);
                        tag.Value = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(tag.Value)));
                    }

                    if (!hasChanges)
                        tag.HasChanges = false;
                }

                EncryptTags(tag.Attributes);
            }
        }

        public void EncryptTags(List<XmlAttribute> attributes)
        {
            foreach (XmlAttribute attribute in attributes)
            {
                if (attribute.IsEncrypted && !string.IsNullOrEmpty(attribute.Value))
                {
                    bool hasChanges = attribute.HasChanges;

                    if (attribute.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_Generic)
                    {
                        attribute.Value = PSE.Framework.Cryptography.RijndaelCryptographer.EncryptString(attribute.Value);
                    }
                    else if (attribute.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.RSA_Infra)
                    {
                        attribute.Value = PSE.Framework.Cryptography.RSACryptographer.EncryptString(attribute.Value, attribute.AlgorithmPublicKey);
                    }
                    else if (attribute.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_InfraData)
                    {
                        attribute.Value = PSE.Deployment.Util.EncryptDecrypt.Encrypt(attribute.Value);
                    }
                    else if (attribute.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.MD5)
                    {
                        var hasher = new PSE.Framework.Cryptography.HashCryptographer(PSE.Framework.Cryptography.HashCryptographer.Algorithm.MD5);
                        attribute.Value = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(attribute.Value)));
                    }

                    if (!hasChanges)
                        attribute.HasChanges = false;
                }
            }
        }

        public void EncryptTags(List<RegistryValue> values)
        {
            foreach (RegistryValue value in values)
            {
                if (value.IsEncrypted && !string.IsNullOrEmpty(value.Value))
                {
                    bool hasChanges = value.HasChanges;

                    if (value.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_Generic)
                    {
                        value.Value = PSE.Framework.Cryptography.RijndaelCryptographer.EncryptString(value.Value);
                    }
                    else if (value.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.RSA_Infra)
                    {
                        value.Value = PSE.Framework.Cryptography.RSACryptographer.DecryptString(value.Value, value.AlgorithmPublicKey);
                    }
                    else if (value.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_InfraData)
                    {
                        value.Value = PSE.Deployment.Util.EncryptDecrypt.Encrypt(value.Value);
                    }
                    else if (value.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.MD5)
                    {
                        var hasher = new PSE.Framework.Cryptography.HashCryptographer(PSE.Framework.Cryptography.HashCryptographer.Algorithm.MD5);
                        value.Value = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(value.Value)));
                    }

                    if (!hasChanges)
                        value.HasChanges = false;
                }
            }
        }

        public void DecryptTags(List<UpdateConfig> updateConfigs)
        {
            foreach (UpdateConfig updateConfig in updateConfigs)
            {
                foreach (XmlFile xmlFile in updateConfig.XmlFiles)
                {
                    DecryptTags(xmlFile.Tags);
                }
            }
        }

        public void DecryptTags(List<XmlTag> tags)
        {
            foreach (XmlTag tag in tags)
            {
                if (tag.IsEncrypted && !string.IsNullOrEmpty(tag.Value))
                {
                    bool hasChanges = tag.HasChanges;

                    if (tag.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_Generic)
                    {
                        tag.Value = PSE.Framework.Cryptography.RijndaelCryptographer.DecryptString(tag.Value);
                    }
                    else if (tag.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.RSA_Infra)
                    {
                        tag.Value = PSE.Framework.Cryptography.RSACryptographer.DecryptString(tag.Value, SinacorPrivateKey);
                    }
                    else if (tag.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_InfraData)
                    {
                        tag.Value = PSE.Deployment.Util.EncryptDecrypt.Decrypt(tag.Value);
                    }

                    if (!hasChanges)
                        tag.HasChanges = false;
                }

                DecryptTags(tag.Attributes);
            }
        }

        public void DecryptTags(List<XmlAttribute> attributes)
        {
            foreach (XmlAttribute attribute in attributes)
            {
                if (attribute.IsEncrypted && !string.IsNullOrEmpty(attribute.Value))
                {
                    bool hasChanges = attribute.HasChanges;

                    if (attribute.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_Generic)
                    {
                        attribute.Value = PSE.Framework.Cryptography.RijndaelCryptographer.DecryptString(attribute.Value);
                    }
                    else if (attribute.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.RSA_Infra)
                    {
                        attribute.Value = PSE.Framework.Cryptography.RSACryptographer.DecryptString(attribute.Value, SinacorPrivateKey);
                    }
                    else if (attribute.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_InfraData)
                    {
                        attribute.Value = PSE.Deployment.Util.EncryptDecrypt.Decrypt(attribute.Value);
                    }

                    if (!hasChanges)
                        attribute.HasChanges = false;
                }
            }
        }

        public void DecryptTags(List<RegistryKey> regKeys)
        {
            foreach (RegistryKey key in regKeys)
            {
                DecryptTags(key.Values);
            }
        }

        public void DecryptTags(List<RegistryValue> values)
        {
            foreach (RegistryValue value in values)
            {
                if (value.IsEncrypted && !string.IsNullOrEmpty(value.Value))
                {
                    bool hasChanges = value.HasChanges;

                    if (value.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_Generic)
                    {
                        value.Value = PSE.Framework.Cryptography.RijndaelCryptographer.DecryptString(value.Value);
                    }
                    else if (value.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.RSA_Infra)
                    {
                        value.Value = PSE.Framework.Cryptography.RSACryptographer.DecryptString(value.Value, SinacorPrivateKey);
                    }
                    else if (value.EncryptionAlgorithm == PSE.Deployment.SetupBase.Configuration.Enums.CryptographyAlgorithm.DES_InfraData)
                    {
                        value.Value = PSE.Deployment.Util.EncryptDecrypt.Decrypt(value.Value);
                    }

                    if (!hasChanges)
                        value.HasChanges = false;
                }
            }
        }
    }
}